package com.main.gateway.interfaces.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.main.common.base.resp.RespBody;
import com.main.common.base.resp.RespCode;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Lin 2025/01/04
 * @version 1.0.0
 * 回退服务
 */
@RestController
public class FallbackController {

    @RequestMapping("/fallback")
    public Map<String, String> fallback() {
        System.out.println("fallback");
        Map<String, String> map = new HashMap<>();
        map.put("code", "-1");
        map.put("message", "服务异常");
        map.put("value", "null");
        return map;
    }

    @RequestMapping("/fallback/requestRateLimiter")
    public ObjectMapper requestRateLimiter() throws JsonProcessingException {
        System.out.println("requestRateLimiter");
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValueAsString(RespCode.TOO_MANY_REQUESTS);
        return mapper;
    }

//    @RequestMapping("/fallback/circuitBreaker")
//    public RespBody<String> circuitBreaker() {
//        return RespBody.fail(RespCode.INTERNAL_SERVER_ERROR);
//    }


//    @RequestMapping("/fallback/circuitBreaker")
//    @ResponseBody
//    public RespBody<Void> handleRequest(HttpServletResponse response) {
//        // 获取响应状态码
//        int statusCode = response.getStatus();
//        response.setStatus(HttpStatus.OK.value());
//        // 返回响应体和状态码
//        return new RespBody<>();
//    }

    @RequestMapping("/fallback/circuitBreaker")
    @ResponseStatus
    public RespBody<Void> circuitBreaker(ServerHttpResponse servletResponse) throws IOException {
        System.out.println(servletResponse);
        return RespBody.fail("fallback");
    }
}
